package com.example.ggpicturegg.myAdapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.ggpicturegg.Imageshow;
import com.example.ggpicturegg.Mysql_com.ImageInfo;
import com.example.ggpicturegg.R;
import com.example.ggpicturegg.ftpUD.UpAndDown;
import com.example.ggpicturegg.tool.BlobAndBytes;
import com.example.ggpicturegg.tool.Loaddata;

import java.io.InputStream;
import java.util.List;
import android.os.Handler;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.LinearViewHolder> {


    private Context mContext;
    private OnItemClickListener mlistener;


    private List<ImageInfo> Image_list ;   //照片信息集合
    private InputStream inputStream;    // 接受 ftp 传过来的数据
    private byte[] bytes;        // 将数据转化成 bytes 类型 然后再转成 bitmap
    private Bitmap bitmap;     // 将bitmap 类型的数据传到 ImageView

    private ImageInfo itemzhu = null;
    private MyAdapter.LinearViewHolder viewHolderzhu = null;
    private int ins;

    private Handler mainHandler;

    private String UserName;



    public void setImage_list(List<ImageInfo> image_list) {
        Image_list = image_list;
    }

    public List<ImageInfo> getImage_list() {
        return Image_list;
    }

    public MyAdapter(){

    }

    public MyAdapter(Context mContext, List<ImageInfo> image_list) {
        this.mContext = mContext;
        this.Image_list = image_list;
    }
    public MyAdapter(Context mContext, List<ImageInfo> image_list,OnItemClickListener listener,String name){
        this.mContext = mContext;
        this.mlistener = listener;
        this.Image_list = image_list;
        this.UserName = name;
    }


    @NonNull
    @org.jetbrains.annotations.NotNull
    @Override
    public MyAdapter.LinearViewHolder onCreateViewHolder(
            @NonNull @org.jetbrains.annotations.NotNull ViewGroup viewGroup, int i) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.list_litm,viewGroup,false));
    }





    // 绑定设置 属性 从数据库
    @Override
    public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull
                                         MyAdapter.LinearViewHolder viewHolder,
                                 @SuppressLint("RecyclerView") int i) {

        ImageInfo item = Image_list.get(i);


        //System.out.println("------ i ---------is       "+i);
        viewHolder.Tv_imageView.setImageBitmap(item.getBitmap());
        viewHolder.Tv_titlel.setText(item.getX_title());
        viewHolder.Tv_username.setText(item.getX_name());
        viewHolder.Tv_number.setText(String.valueOf(item.getX_coutn()));

        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                mlistener.OnClick(i);
                Intent inte = new Intent(mContext, Imageshow.class);
                Bundle bundle = new Bundle();
                bundle.putString("path",item.getPicture_name());
                bundle.putInt("id",item.getId());
                bundle.putString("user_name",item.getX_name());
                bundle.putString("title",item.getX_title());
                bundle.putString("desc",item.getDesc());
                bundle.putString("UserName",UserName);
                inte.putExtras(bundle);
                mContext.startActivity(inte);
            }
        });
    }

    @Override
    public int getItemCount() {
        return Image_list.size();
    }

    class LinearViewHolder extends RecyclerView.ViewHolder{

        private ImageView Tv_imageView;
        private TextView Tv_titlel;
        private TextView Tv_username;
        private TextView Tv_number;
        public LinearViewHolder( View itemView) {
            super(itemView);
            Tv_imageView = itemView.findViewById(R.id.imagephoto);
            Tv_titlel = itemView.findViewById(R.id.phototitle);
            Tv_username = itemView.findViewById(R.id.username);
            Tv_number = itemView.findViewById(R.id.number);
        }
    }

    public interface  OnItemClickListener{
        void OnClick(int pos);
    }
}
